<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-input v-model="state.queryForm.tableName" clearable placeholder="请输入表名"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="importHandle()">导入</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="success" @click="downloadBatchHandle()">生成代码</el-button>
			</el-form-item>
			<el-form-item>
				<el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
			</el-form-item>
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%" @selection-change="selectionChangeHandle">
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column type="index" label="序号" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="projectName" label="项目名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="tableName" label="表名" show-overflow-tooltip header-align="center" align="center" width="180"></el-table-column>
			<el-table-column prop="tableComment" label="注释" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="320">
				<template #default="scope">
					<el-button type="primary" link @click="generatorHandle(scope.row.id)">编辑</el-button>
					<el-button type="primary" link @click="generatorCode(scope.row)">生成代码</el-button>
					<el-button type="primary" link @click="editHandle(scope.row.id)">字段配置</el-button>
					<el-button type="primary" link @click="previewHandle(scope.row.id)">预览</el-button>
					<el-dropdown style="margin-top: 2px">
						<el-button type="primary" link>
							更多
							<el-icon>
								<arrow-down />
							</el-icon>
						</el-button>
						<template #dropdown>
							<el-dropdown-menu>
								<el-dropdown-item @click="syncHandle(scope.row)">同步</el-dropdown-item>
								<el-dropdown-item @click="deleteBatchHandle(scope.row.id)">删除</el-dropdown-item>
							</el-dropdown-menu>
						</template>
					</el-dropdown>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>

		<import ref="importRef" @refresh-data-list="getDataList"></import>
		<preview ref="previewRef" @refresh-data-list="getDataList"></preview>
		<field-config ref="editRef" @refresh-data-list="getDataList"></field-config>
		<update ref="generatorRef" @refresh-data-list="getDataList"></update>
	</el-card>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { useCrud } from '@/hooks'
import Import from './import.vue'
import FieldConfig from './field-config.vue'
import Preview from './preview.vue'
import Update from './update.vue'
import { useTableSyncApi } from '@/api/table'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDownloadLocalApi, useDownloadApi } from '@/api/generator'
import { ArrowDown } from '@element-plus/icons-vue'

const state: IHooksOptions = reactive({
	dataListUrl: '/gen/table/page',
	deleteUrl: '/gen/table',
	queryForm: {
		tableName: ''
	}
})

const importRef = ref()
const editRef = ref()
const previewRef = ref()
const generatorRef = ref()

const importHandle = (id?: number) => {
	importRef.value.init(id)
}

const editHandle = (id?: number) => {
	editRef.value.init(id)
}
const previewHandle = (tableId?: number) => {
	previewRef.value.init(tableId)
}
const generatorHandle = (id?: number) => {
	generatorRef.value.init(id)
}

const downloadBatchHandle = () => {
	const tableIds = state.dataListSelections ? state.dataListSelections : []

	if (tableIds.length === 0) {
		ElMessage.warning('请选择生成代码的表')
		return
	}

	useDownloadApi(tableIds)
}

const syncHandle = (row: any) => {
	ElMessageBox.confirm(`确定同步数据表${row.tableName}吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			useTableSyncApi(row.id).then(() => {
				ElMessage.success('同步成功')
			})
		})
		.catch(() => {})
}

const generatorCode = item => {
	const id = item.id
	if (item.generatorType === 0) {
		useDownloadApi([id])
	} else if (item.generatorType === 1) {
		useDownloadLocalApi(id).then(() => {
			ElMessage.success({
				message: '代码已经下载到本地',
				duration: 1000
			})
		})
	}
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
</script>
